package com.wd.health.view.activity.home;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;

import com.wd.health.R;
import com.wd.health.view.fragment.CjbzFragment;
import com.wd.health.view.fragment.CyypFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class CjbzActivity extends AppCompatActivity {

    private ViewPager vp;
    private TabLayout tl;
    private List<String> slist = new ArrayList<>();
    private List<Fragment> flist = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cjbz);
        initView();
        initData();
        Intent intent = getIntent();
        boolean id = intent.getBooleanExtra("id", false);
        if (id==true){
            vp.setCurrentItem(1);
        }
    }

    private void initData() {
        if (slist.size()<1){
            slist.add("常见病症");
            slist.add("常用药品");
            flist.add(new CjbzFragment());
            flist.add(new CyypFragment());
        }
        vp.setAdapter(new CjbzPagerAdapter(getSupportFragmentManager()));
        tl.setupWithViewPager(vp);
    }

    private void initView() {
        vp = findViewById(R.id.vp_cjbz);
        tl = findViewById(R.id.tl_cjbz);
        tl.setSelectedTabIndicator(0);
    }

    private class CjbzPagerAdapter extends FragmentPagerAdapter {
        public CjbzPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return flist.get(position);
        }

        @Override
        public int getCount() {
            return flist.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return slist.get(position);
        }
    }
}